package cn.frh.service.impl;

import cn.frh.domain.Product;
import cn.frh.domain.Store;
import cn.frh.dto.ProDto;
import cn.frh.feign.StoreService;
import cn.frh.mapper.ProductMapper;
import cn.frh.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.JsonResult;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private StoreService storeService;

    @Override
    @Transactional
    public JsonResult add(ProDto proDto) {
        try {
            Product product = new Product();
            product.setName(proDto.getName());
            product.setPrice(proDto.getPrice());
            productMapper.add(product);
            Store store = new Store();
            store.setProductId(product.getId());
            store.setNumber(proDto.getNumber());
            JsonResult add = storeService.add(store);
            if (!add.getSuccess()){
                return add;
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResult.me();
    }
}
